<?php

include '../../authorization_check.php';
include '../../checkPCandVPC.php';

require_once '../../commonutils/util/PageTemplate.php';
require_once '../../commonutils/db/DBConnectionManager.php';

function addJavaScripts($webPage){
	$javaScript = '

		function checkAddNewPaymentForm(){
			
			var paymentPerHour = window.document.addNewPaymentForm.textPaymentPerHour;
   			var name = window.document.addNewPaymentForm.selectName;
				
		   	if(name.value == -1){
		   		alert("First select an instructor");
		   		name.focus();
		   		return false;
		   	}
		   	
		   	if(paymentPerHour.value == ""){
		   		alert("Payment Per Hour field can not be empty");
		   		paymentPerHour.focus();
		   		return false;
		   	}
		   	
    		return true;
		}
		
		';
	$webPage->addJavaScriptContent($javaScript);
	$webPage->addCommonJavaScripts();
}

function addAddNewPaymentForm($webPage){
	$webPage->addContent("<form name='addNewPaymentForm' method='post' action=''>");
	$webPage->addContent("<table align = 'center' border=1 name='addPaymentTable'>");
	$webPage->addContent("<tr>");
		$webPage->addContent("<th width=243 align=left>Name: </th>");
		$webPage->addContent("<td align=left width=343><select size=1 name=selectName>");
		$webPage->addContent("<option value=-1>-Select A Personnel-</option>");
			
			$result = DBConnectionManager::query("select instructorID, firstName, lastName, title from Instructor where programCode=$_SESSION[programCode]");
			
			while($row = mysql_fetch_assoc($result)){
				$instructorID = $row['instructorID']; 
				
				$numberOfRows = DBConnectionManager::num_of_rows("select * from InstructorPayment where instructorID=$instructorID");
				
				//if a payment for this personnel has not been added before?
				if($numberOfRows == 0){
					$firstName = $row['firstName']; 
					$lastName = $row['lastName']; 
					$title = $row['title']; 
					$webPage->addContent("<option value=$instructorID>$title $firstName $lastName</option>");	
				}
			}
		$webPage->addContent("</select></td>");
	$webPage->addContent("</tr>");
	$webPage->addContent("<tr>");
		$webPage->addContent("<th align=left>Payment Per Hour (YTL):</th>");
		$webPage->addContent("<td align=left><input type=text name=textPaymentPerHour onKeyPress='numericalOnly()'</td>");
	$webPage->addContent("</tr>");
	$webPage->addContent("<tr>");
		$webPage->addContent("<th align=left>Number Of Weeks:</th>");
		$webPage->addContent("<td align=left width=343>");
		addNumberOfWeeksCombobox($webPage, 0, 14, '');
		$webPage->addContent("</td>");
	$webPage->addContent("</tr>");
	$webPage->addContent("<tr>");
		$webPage->addContent("<td align=center colspan=2><input type=submit value='Add' onClick = 'return checkAddNewPaymentForm();' name=submitAdd><input type=reset value=Reset name=reset></td>");
	$webPage->addContent("</tr></table></form>");
}

function addNumberOfWeeksCombobox($webPage, $formIndex, $defaultValue, $finalized){
	
	$webPage->addContent("<select size=1 name=selectNumberOfWeeks[] $finalized>");
		
	for($i = 10; $i < 21; $i++){
		
		if($i != $defaultValue){
			$webPage->addContent("<option value=$i>$i</option>");
		}else{
			$webPage->addContent("<option selected value=$i>$i</option>");
		}
	}
		
	$webPage->addContent("</select>");
}

function listPayments($webPage){
	$webPage->addContent("<form name='paymentListForm' method='post'>");
	$webPage->addContent("<table align = 'center' border=1 name='listTable'>");
	$webPage->addContent("<tr>");
		$webPage->addContent("<th>Select</th>");
		$webPage->addContent("<th>ID</th>");
		$webPage->addContent("<th>Instructor</th>");
		$webPage->addContent("<th>Payment Per Hour (YTL)</th>");
		$webPage->addContent("<th>Lecture Hour Per Week</th>");
		$webPage->addContent("<th>Number Of Weeks</th>");
		$webPage->addContent("<th>Lecture Hour For Semester</th>");
		$webPage->addContent("<th>Total Payment For Lectures (YTL)</th>");
		$webPage->addContent("<th>Final Exam Payments (YTL)</th>");
		$webPage->addContent("<th>Total Payment (YTL)</th>");
	$webPage->addContent("</tr>");
	
		$result = DBConnectionManager::query("select * from Instructor i, InstructorPayment ip where i.instructorID=ip.instructorID and i.programCode=$_SESSION[programCode] and ip.semesterID=$_SESSION[semesterID]");
		$totalPayment = 0;
		
		while($row = mysql_fetch_assoc($result)){
			$instructorID = $row['instructorID']; 
			$firstName = $row['firstname']; 
			$lastName = $row['lastname']; 
			$title = $row['title'];
			$week = $row['numberofweeks'];
			$paymentPerHour = $row['paymentperhour'];
			$finalized = ($row['finalized'] == 1) ? "disabled" : "";
			$lectureHourPerWeek = getLectureHour($instructorID);
			$lectureHourPerSemester = $lectureHourPerWeek * $week;
			$totalPaymentForLecture = $lectureHourPerSemester * $paymentPerHour;
			$finalExamPayment = getFinalExamCount($instructorID) * 10;
			$totalPaymentForAStaff = $totalPaymentForLecture + $finalExamPayment; 
			$totalPayment += $totalPaymentForAStaff;
			
			$webPage->addContent("<tr>");
				$webPage->addContent("<td><input type=checkbox name='checkBoxSelect[]' value=$instructorID $finalized></td>");
				$webPage->addContent("<td><input type=hidden name='instructorID[]' value=$instructorID>$instructorID </td>");
				$webPage->addContent("<td>$title $firstName $lastName</td>");
				$webPage->addContent("<td align=left><input type=text name=textPaymentPerHour[] value=$paymentPerHour $finalized></td>");
				$webPage->addContent("<td align=left>$lectureHourPerWeek</td>");
				$webPage->addContent("<td>");
					addNumberOfWeeksCombobox($webPage, 1, $week, $finalized);
				$webPage->addContent("</td>");
				$webPage->addContent("<td align=left>$lectureHourPerSemester</td>");
				$webPage->addContent("<td align=left>$totalPaymentForLecture</td>");
				$webPage->addContent("<td align=left>$finalExamPayment</td>");
				$webPage->addContent("<td>$totalPaymentForAStaff</td>");	
			$webPage->addContent("</tr>");
		}
	$webPage->addContent("<tr><td colspan = 8/></td><th align=left>TOTAL:</th><th align=left>$totalPayment</th>");
	$webPage->addContent("</tr>");
	$webPage->addContent("</table>");
	$webPage->addContent("<table align = 'center'><tr>");
	$webPage->addContent("<td>");
	$webPage->addContent("<input type=submit value='Remove Selected(s)' name=submitRemove onClick= 'return confirmDelete()'>");
	$webPage->addContent("</td>");
	$webPage->addContent("<td>");
	$webPage->addContent("<input type=submit value='Finalize' name=submitFinalize>");
	$webPage->addContent("</td>");
	$webPage->addContent("<td>");
	$webPage->addContent("<input type=submit value='Update' name=submitUpdate>");
	$webPage->addContent("</td>");
	$webPage->addContent("<td>");
	$webPage->addContent("<input type=reset value='Reset' name=resetReset>");
	$webPage->addContent("</td>");
	$webPage->addContent("</tr></table></form>");
}

function getFinalExamCount($instructorID){
	
	$result = DBConnectionManager::query("select count(distinct c.courseCode) as CourseCount from course c, section s where 
		s.instructorID=$instructorID and
		s.semesterID=$_SESSION[semesterID] and
		c.courseCode=s.courseCode and
		c.programCode=$_SESSION[programCode]
		");
		
	$finalCount = 0;
		
	if($row = mysql_fetch_assoc($result)){
		$finalCount = $row[CourseCount];	
	}
	
	return $finalCount;
}

function getLectureHour($instructorID){
	
	$result = DBConnectionManager::query("select sum(hours) as LectureHour from course c, section s where 
		s.instructorID=$instructorID and
		s.semesterID=$_SESSION[semesterID] and
		c.courseCode=s.courseCode and
		c.programCode=$_SESSION[programCode]
		");
		
	$weeklyHours = 0;
		
	if($row = mysql_fetch_assoc($result)){
		$weeklyHours = $row[LectureHour];	
	}
	
	$result = DBConnectionManager::query("select title from Instructor where instructorID=$instructorID");
	
	$titileAdd = 0;
	
	if($row = mysql_fetch_assoc($result)){
		$title = $row["title"];

		if($title == "Prof. Dr." || $title == "Assoc. Prof. Dr."){
			$titileAdd = 2;
		}else if($title == "Assist. Prof. Dr."){
			$titileAdd = 3;
		}else{ 
			$titileAdd = 4;
		}
	}
	
	$result = DBConnectionManager::query("select count(distinct stu.studentID) as StudentCount from student stu, enrollment e, termproject t, section sec where 
		sec.instructorID=$instructorID and
		sec.semesterID=$_SESSION[semesterID] and
		t.sectionID=sec.sectionID and
		t.programCode=$_SESSION[programCode] and
		stu.programCode= $_SESSION[programCode] and
		e.studentID=stu.studentID and
		e.sectionID=sec.sectionID and
		e.semesterID=$_SESSION[semesterID]");

	$stundentsInProjects = 0;
	
	if($row = mysql_fetch_assoc($result)){
		$stundentsInProjects = intval(($row['StudentCount'] / 2));	
	}
	
	$result = DBConnectionManager::query("select count(distinct stu.studentID) as StudentCount from student stu, enrollment e, course c, section sec where 
		sec.instructorID=$instructorID and
		sec.semesterID=$_SESSION[semesterID] and
		c.courseCode=sec.courseCode and
		c.programCode=$_SESSION[programCode] and
		stu.programCode= $_SESSION[programCode] and
		e.studentID=stu.studentID and
		e.sectionID=sec.sectionID and
		e.semesterID=$_SESSION[semesterID]");
			
	$totalStudentCount = 1;
	
	if($row = mysql_fetch_assoc($result) && $row['StudentCount'] > 40){
		$totalStudentCount = 2;
	}
	
	return $weeklyHours + $titileAdd + $stundentsInProjects + $totalStudentCount;
}

function addNewPayment(){
	
	$instructorID = $_POST['selectName'];
	$numberOfWeeks = $_POST['selectNumberOfWeeks'];
	$paymentPerHour = $_POST['textPaymentPerHour'];
	
	DBConnectionManager::query("insert into InstructorPayment(paymentPerHour, numberOfWeeks, instructorID, semesterID) values($paymentPerHour, $numberOfWeeks[0], $instructorID, $_SESSION[semesterID])");
}

function removeSelectedPayments($webPage){
	
	$selectedCheckBoxes = $_POST['checkBoxSelect'];
	
	if(count($selectedCheckBoxes) == 0){
		$webPage->addJavaScriptContent("alert(\"You should first select a payament to remove\");");
		return;
	}
	
	foreach($selectedCheckBoxes as $aSelectedPaymentID){
		DBConnectionManager::query("delete InstructorPayment ip from Instructor i, InstructorPayment ip 
				where i.instructorID=ip.instructorID and
		 		ip.instructorID=$aSelectedPaymentID and 
		 		i.programCode=$_SESSION[programCode] and 
		 		ip.semesterID=$_SESSION[semesterID]");
	}
}

function updatePayments($webPage){
	
	$instructorIDs = $_POST['instructorID'];
	
	for($i = 0; $i < count($instructorIDs); ++$i ){
		$instructorID = $instructorIDs[$i];
		$paymentPerHour = $_POST['textPaymentPerHour'][$i];
		$numberOfWeeks = $_POST['selectNumberOfWeeks'][$i];
		
		DBConnectionManager::query("update InstructorPayment set numberofweeks=$numberOfWeeks, paymentperhour=$paymentPerHour where instructorID=$instructorID ");
	}
}

function finalizePayments(){
	
	$instructorIDs = $_POST['instructorID'];
	
	for($i = 0; $i < count($instructorIDs); ++$i ){
		$instructorID = $instructorIDs[$i];
		DBConnectionManager::query("update InstructorPayment set finalized=1 where instructorID=$instructorID");
	}
}

include "Top.html";
include 'notifier.php';

$webPage = new Page();

addJavaScripts($webPage);

$webPage->addHeader('Instructor Payment Details');

if(isset($_POST['submitAdd'])){
	addNewPayment();
}else if(isset($_POST['submitRemove'])){
	removeSelectedPayments($webPage);
}else if(isset($_POST['submitUpdate'])){
	updatePayments($webPage);
}else if(isset($_POST['submitFinalize'])){
	finalizePayments();
}

addAddNewPaymentForm($webPage);
listPayments($webPage);
$webPage->addFooter(date('Y'), 'IIPIS');
echo $webPage->get();
include 'bottom.php';
?>